package com.sinosoft.hydra.um.services.component.dataimport.validators;

import com.sinosoft.hydra.um.services.component.dataimport.resolvers.ValidateResult;

/**
 * Created by Administrator on 2015/1/10.
 */
public class MaxNumberValidator extends Validator {

    private Number maxNumber;
    private String errorMsg;

    public MaxNumberValidator(Number maxNumber) {
        this.maxNumber = maxNumber;
    }

    public MaxNumberValidator(Number maxNumber, String errorMsg) {
        this.maxNumber = maxNumber;
        this.errorMsg = errorMsg;
    }

    public ValidateResult validate(Object object) {
        if (object == null) {
            return ValidateResult.PASS();
        }
        try {
            Number ss = (Number) object;
            if (ss.doubleValue() > maxNumber.doubleValue()) {
                if (errorMsg != null) {
                    return ValidateResult.ERROR(errorMsg);
                }
                return ValidateResult.ERROR("数字的最大值不能超过" + maxNumber);
            }
        } catch (Exception ex) {
            throw new ValidateException();
        }

        return ValidateResult.PASS();
    }
}
